package controller;

import pojo.Emp;
import service.EmpServiceImpl;
import service.IEmpService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class EmpController extends HttpServlet {
    private IEmpService ies = new EmpServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String requestURI = req.getRequestURI();
        int index = requestURI.lastIndexOf("/")+1;
        String mName = requestURI.substring(index);

        Class<EmpController> cla = EmpController.class;
        try {
            //根据方法名获得方法
            Method method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            //执行方法
            method.invoke(this,req,resp);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
    public void emplogin(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException{
        resp.setContentType("text/html;charset=utf-8");
        //接用户的参数值
        String empno = req.getParameter("empno");
        String ename = req.getParameter("ename");
        //根据编号和姓名获取对象
        Emp login = ies.Login(new Emp(Integer.parseInt(empno), ename));
        if(login != null){
            ArrayList<Emp> emps = ies.queryAll();
            resp.getWriter().println("<html>");
            resp.getWriter().println("<body>");
            resp.getWriter().println("<table>");
            resp.getWriter().println("<tr><td>员工编号</td><td>员工姓名</td><td>岗位名称</td><td>工资</td><td>操作</td></tr>");
            for(Emp e:emps){
                resp.getWriter().println("<tr><td>"+e.getEmpno()+"</td><td>"+e.getEname()+"</td><td>"+e.getJob()+"" +
                        "</td><td>"+e.getSal()+"</td><td><a href=empdel?empno="+e.getEmpno()+" >删除</td></tr>");
            }
            resp.getWriter().println("</table>");
            resp.getWriter().println("</body>");
            resp.getWriter().println("</html>");

        }else{
            resp.getWriter().println("<script type='text/javascript'> alert('登录失败！not ok') </script>");
        }


    }

    public void empdel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        String empno = req.getParameter("empno");
        boolean b = ies.removeByEmpno(Integer.parseInt(empno));
        if(b){
            resp.getWriter().println("<script type='text/javascript'> alert('删除成功') </script>");
        }else{
            resp.getWriter().println("<script type='text/javascript'> alert('删除失败') </script>");
        }
    }
}
